/********************************************************************************
 * UserServiceDelegator.java
 *
 * Created by Ankur Jain
 * Last modified Date: Aug 26, 2008 11:13:51 AM by Ankur
 * Version : 0.1
 * Copyright (c) 2008 - 2009 cyberThink Infotech Pvt. Ltd., All rights reserved.
 ********************************************************************************/
package com.printeverywhere.delegator;

import static com.printeverywhere.util.AttributeConstants.CLOSING_SESSION;

import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.apache.log4j.Logger;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import com.printeverywhere.dto.CreditCardType;
import com.printeverywhere.util.AttributeConstants;
import com.printeverywhere.util.HibernateUtil;
import com.printeverywhere.util.SearchCriteria;

// TODO: Auto-generated Javadoc
/**
 * The Class CreditCardTypeServiceDelegator.
 */
public class CreditCardTypeServiceDelegator {
	
	/** The log. */
	private static Logger log = Logger.getLogger(CreditCardTypeServiceDelegator.class);

	/**
	 * Gets the list.
	 * 
	 * @return the list
	 * 
	 * @throws HibernateException the hibernate exception
	 */
	@SuppressWarnings("unchecked")
	public static List<CreditCardType> getList() throws HibernateException {
		if(log.isInfoEnabled()){
			log.info("Getting list for Bank Account without search criteria".intern());
		}
		Session session = HibernateUtil.getSession();
		try {
			Transaction transaction = session.beginTransaction();
			List<CreditCardType> list = session.createCriteria(CreditCardType.class).list();
			transaction.commit();
			return list;
		} catch (HibernateException e) {
			log.error("Got exception when getting list for Bank Account without search criteria".intern());
			throw e;
		} finally {
			if(log.isInfoEnabled()){
				log.info(CLOSING_SESSION);
			}
			session.flush();
			session.close();
		}
	}
	
	
	/**
	 * Gets the credit card type.
	 * 
	 * @param id the id
	 * 
	 * @return the credit card type
	 * 
	 * @throws HibernateException the hibernate exception
	 */
	public static CreditCardType getCreditCardType(int id) throws HibernateException {
		if(log.isInfoEnabled()){
			log.info("Getting list for creditCardType without search criteria".intern());
		}
		Session session = HibernateUtil.getSession();
		try {
			Transaction transaction = session.beginTransaction();
			Criteria criteria = session.createCriteria(CreditCardType.class);
			criteria.add(Restrictions.eq(AttributeConstants.ID, id));
			List<CreditCardType> groupList = criteria.list();
			transaction.commit();
			return (groupList.size() == 0) ? new CreditCardType() : groupList.get(0);
		} catch (HibernateException e) {
			log.error("Got exception when getting list for creditCardType without search criteria".intern());
			throw e;
		} finally {
			if(log.isInfoEnabled()){
				log.info(CLOSING_SESSION);
			}
			session.flush();
			session.close();
		}
	}

	/**
	 * Gets the list.
	 * 
	 * @param searchCriteria the search criteria
	 * 
	 * @return the list
	 * 
	 * @throws HibernateException the hibernate exception
	 */
	public static List<CreditCardType> getList(SearchCriteria searchCriteria)throws HibernateException {
		if(log.isInfoEnabled()){
		log.info("Getting list for creditCardType with search criteria".intern());
		}
		Session session = HibernateUtil.getSession();
		Criteria criteria = session.createCriteria(CreditCardType.class);
		String orderBY = searchCriteria.getOrderBy();
		if (orderBY != null) {
			if (searchCriteria.isAsc()) {
				criteria.addOrder(Order.asc(orderBY));
			} else {
				criteria.addOrder(Order.desc(orderBY));
			}
		}
		Set<String> keys = searchCriteria.getAttributeNames();
		Iterator<String> it = keys.iterator();
		String key = null;
		Object value = null;
		while (it.hasNext()) {
			key = it.next();
			value = searchCriteria.getAttribute(key);
			criteria.add(Restrictions.eq(key, value));
		}
		Transaction transaction = session.beginTransaction();
		List<CreditCardType> list;
		try {
			list = criteria.list();
			transaction.commit();
		} catch (HibernateException e) {
			log.error("Got exception when getting list for creditCardType with search criteria".intern());
			throw e;
		} finally {
			if(log.isInfoEnabled()){
				log.info(CLOSING_SESSION);
			}
			session.flush();
			HibernateUtil.closeSession();
		}
		return list;
	}
	
	
	

	/**
	 * Insert credit card type.
	 * 
	 * @param creditCardType the credit card type
	 * 
	 * @throws HibernateException the hibernate exception
	 */
	public static void insertCreditCardType(CreditCardType creditCardType) throws HibernateException {
		if(log.isInfoEnabled()){
			log.info("Called insertCreditCardType method".intern());
		}
		Session session = HibernateUtil.getSession();
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			session.saveOrUpdate(creditCardType);
			tx.commit();
		} catch (RuntimeException e) {
			log.error("Got exception when insert creditCardType with search criteria".intern());
			if (tx != null)
				tx.rollback();
			throw e;
		} finally {
			if(log.isInfoEnabled()){
				log.info(CLOSING_SESSION);
			}
			session.flush();
			session.close();
		}
	}

	/**
	 * Delete credit card type.
	 * 
	 * @param creditCardType the credit card type
	 * 
	 * @throws HibernateException the hibernate exception
	 */
	public static void deleteCreditCardType(CreditCardType creditCardType) throws HibernateException {
		if(log.isInfoEnabled()){
			log.info("Called deletecreditCardType method".intern());
		}
		Session session = HibernateUtil.getSession();
		Transaction transaction = null;
		try {
			transaction = session.beginTransaction();
			session.delete(creditCardType);
			transaction.commit();
		} catch (RuntimeException e) {
			log.error("Got exception when Delete creditCardType".intern());
			if (transaction != null)
				transaction.rollback();
			throw e;
		} finally {
			if(log.isInfoEnabled()){
				log.info(CLOSING_SESSION);
			}
			session.flush();
			session.close();
		}
	}	
}